﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Entity;
using com.iss.inventory.BusinessLogic;
using com.iss.inventory.CustomModels;
using com.iss.inventory.Controllers;
using System.Drawing;

namespace Staionery_Inventory.Presentation.StockCard
{
    public partial class AddItemToVoucherUI__Reload_ : System.Web.UI.Page
    {
        AddItemToVoucherController addCon = new AddItemToVoucherController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }
            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }


            int itemID;
            List<StationeryItem> items = new List<StationeryItem>();
            if (!IsPostBack)
            {
                txtQtyAdjusted.Text = "0";
                if (Session["Search_allselecteddata"] != null)
                {
                    items = Session["Search_allselecteddata"] as List<StationeryItem>;

                    if (items.Count != 0)//handle when no selected item process
                    {
                        itemID = items[0].ItemID;
                        String[] item = addCon.getItemByItemCode(Convert.ToString(itemID));
                        lblBinNo.Text = item[0]; lblItemID.Text = item[1]; lblName.Text = item[2]; lblQtyLeft.Text = item[3];
                        txtQtyAdjusted.ReadOnly = false; ddlReason.Enabled = true;
                    }
                }
            }
        }

        protected void btnGetItem_Click(object sender, EventArgs e)
        {
            if (txtGetItem.Text != "")
            {
                if (rbtByBinNo.Checked)
                {
                    String[] item = addCon.getItemByBinNo(txtGetItem.Text);
                    if (item == null)
                    {
                        lblMessage.ForeColor = Color.Red;
                        lblMessage.Text = "Please enter a correct Bin No";
                    }
                    else
                    {
                        lblBinNo.Text = item[0]; lblItemID.Text = item[1]; lblName.Text = item[2]; lblQtyLeft.Text = item[3];
                        lblMessage.ForeColor = Color.Black;
                        lblMessage.Text = "Load Successfully";
                        txtQtyAdjusted.ReadOnly = false;
                        ddlReason.Enabled = true;
                        txtGetItem.Text = "";
                        txtGetItem.BackColor = Color.White;
                    }
                }
                else
                {
                    String[] item = addCon.getItemByItemCode(txtGetItem.Text);
                    if (item == null)
                    {
                        lblMessage.ForeColor = Color.Red;
                        lblMessage.Text = "Please enter a correct Item Code";
                    }
                    else
                    {
                        lblBinNo.Text = item[0]; lblItemID.Text = item[1]; lblName.Text = item[2]; lblQtyLeft.Text = item[3];
                        lblMessage.ForeColor = Color.Black;
                        lblMessage.Text = "Load Successfully";
                        txtQtyAdjusted.ReadOnly = false;
                        ddlReason.Enabled = true;
                        txtGetItem.Text = "";
                        txtGetItem.BackColor = Color.White;
                    }
                }
            }
            else
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "Please enter an item code or a bin no or use the 'Search' button to get an item";
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (txtQtyAdjusted.Text == "")
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "Please enter a number";
            }
            else if (ddlReason.Text == "Select a Reason")
            {

                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "Please select a reason";
            }
            else
            {
                String result = addCon.addAdjustmentVoucher(lblItemID.Text, txtQtyAdjusted.Text, ddlReason.Text, DateTime.Now);
                lblMessage.ForeColor = Color.Black;
                lblMessage.Text = result;
                txtQtyAdjusted.Text = "0"; ddlReason.Text = "Select a Reason";
                txtQtyAdjusted.ReadOnly = true; ddlReason.Enabled = false;
                lblBinNo.Text = ""; lblItemID.Text = ""; lblName.Text = ""; lblQtyLeft.Text = "";

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            txtGetItem.Text = ""; txtQtyAdjusted.Text = "";
            SearchItemsModel model = new SearchItemsModel();
            model.SingleSelectMode = true;
            Session.Remove("searchitemsmodel");
            Session["searchitemsmodel"] = model;
            Response.Redirect("~/Presentation/Search/SearchItemsPage.aspx");
        }
    }
}